﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using NGen;
using NGen.Managers.GameObject;
using NGen.Systems.Graphics;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using NGen.Managers.Collision;
using NGen.Systems.StringAlarms;
using Psych.Objects.Enemies;
using Psych.Misc;
using Microsoft.Xna.Framework.Audio;
using NGen.Managers.Audio;

namespace Psych.Objects.Character
{
    class GameObjectPlayer : GameObject2D, ICollidable2D, IAlarmListener
    {
        #region properties
        
        public Vector2 position { get; protected set; }

        #endregion

        #region fields

        public AnimatedSprite player;
        public AnimatedSprite idle_player;
        public AnimatedSprite running_player;
        public AnimatedSprite punch_player;
        public Point frameSize;
        private SpriteEffects spriteEffect;
        private SpriteBatch spriteBatch;
        private BoundingBox m_boundingBox;
        private SoundEffect punchSound;
        //private SoundEffect runSound;
        //private AlarmSystem punchAlarm;
        
        #endregion

        #region GameObject2D implementation

        /// <summary>
        /// 	_Initialize this enemy
        /// </summary>
        protected override void _Initialize()
        {
            spriteBatch = new SpriteBatch(GameEngine.GraphicsDevice);
            frameSize = new Point(191, 150);
            position = new Vector2(250, 250);
            m_boundingBox = new BoundingBox(new Vector3(), new Vector3(191, 150, 1.0f));
            punchSound = GameEngine.GameContent.Load<SoundEffect>("Content\\Sounds\\PUNCH");
            //runSound = GameEngine.GameContent.Load<SoundEffect>("Content\\Sounds\\runsound");
            //punchAlarm = new AlarmSystem(); 


            idle_player = new AnimatedSprite(GameEngine.GameContent.Load<Texture2D>("Content\\Player\\idle"), frameSize, new Point(1, 1), 1.0f);
            running_player = new AnimatedSprite(GameEngine.GameContent.Load<Texture2D>("Content\\Player\\main_ss_run"), frameSize, new Point(4, 1), 0.25f);
            punch_player = new AnimatedSprite(GameEngine.GameContent.Load<Texture2D>("Content\\Player\\punch"), frameSize, new Point(1, 1), 1.0f);

            m_position.X = 275;
            m_position.Y = 325;
            m_zDepth = 0.999f;
            
            if (m_texture != null)
            {
                m_textureOffset.X = m_texture.Height / 2.0f;
                m_textureOffset.Y = m_texture.Width / 2.0f;
            }

            spriteEffect = SpriteEffects.FlipHorizontally;
            
            base._Initialize();
        }

        /// <summary>
        /// 	Update this enemy for this frame
        /// </summary>
        /// <param name = "p_gameTime"></param>
        protected override void _Update(GameTime p_gameTime)
        {
            KeyboardState keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Keys.Right))
            {
                player = running_player;
                spriteEffect = SpriteEffects.FlipHorizontally;
                //runSound.Play();
            }
            else if (keyState.IsKeyDown(Keys.Left))
            {
                player = running_player;
                spriteEffect = SpriteEffects.None;
            }
            else if (keyState.IsKeyDown(Keys.Space))
            {
                player = punch_player;
                punchSound.Play();
            }

            else if (keyState.IsKeyUp(Keys.Right) && keyState.IsKeyUp(Keys.Left) && keyState.IsKeyUp(Keys.Space))
            {
                player = idle_player;
            }

            player.Update(p_gameTime);
            
            //destroy this object if it is low on health
            /*if (health <= 0)
            {

                Destroy(Id);
            }*/

        }

        protected override void _Draw(GameTime p_gameTime)
        {
            GameEngine.SpriteBatch.Draw(player.AnimationSpriteSheet, m_position, player.GetFrame(), Color.White, 0.0f, new Vector2(0, 0), 1.0f, spriteEffect, 0.999f);
        }

        protected override void _Cleanup()
        {
            GameEngine.GetService<IManagerCollision2D>().UnRegisterCollidable(Id);
            GameEngine.GetService<IManagerGameObjects>().DestroyGameObjectById(Id);
        }

        #endregion





        public BoundingBox BoundingBox
        {
            get { return m_boundingBox; }
            set { }
        }

        public void Collided(CollisionMessage2D p_objectCollidedWith)
        {
            if ((p_objectCollidedWith.m_objectType == typeof(GameObjectSlender)) ||
                (p_objectCollidedWith.m_objectType == typeof(GameObjectStraightJacket)))
            {
               GameEngine.GetService<GameStats>().Health += -1;
            }
        }

        public void OnAlarmEvent(string p_message)
        {
            throw new NotImplementedException();
        }
    }
}
